package com.zhongxi.guoyuan.widget.screenadaptaion;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RelativeLayout;

import com.zhongxi.guoyuan.R;

/**
 * Created by Administrator on 2018/1/30.
 */

public class CustomRelativelayout extends RelativeLayout {
    public CustomRelativelayout(Context context) {
        super(context);
    }

    public CustomRelativelayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomRelativelayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int mChildCount = this.getChildCount();
        for (int i = 0; i < mChildCount; i++) {
            View mChildAt = this.getChildAt(i);
            RelativeLayout.LayoutParams mLayoutParams = (RelativeLayout.LayoutParams) mChildAt.getLayoutParams();  //父view 为linearLayout
            float widthParent = 0;
            float heightParent = 0;
            float topMargin = 0;
            float bottomMargin = 0;
            float rightMargin = 0;
            float leftMargin = 0;
            if (mLayoutParams instanceof LayoutParams) {
                widthParent = ((LayoutParams) mLayoutParams).getWidthParent();
                heightParent = ((LayoutParams) mLayoutParams).getHeightParent();
                topMargin = ((LayoutParams) mLayoutParams).getTopMargin();
                bottomMargin = ((LayoutParams) mLayoutParams).getBottomMargin();
                rightMargin = ((LayoutParams) mLayoutParams).getRightMargin();
                leftMargin = ((LayoutParams) mLayoutParams).getLeftMargin();
            }
            if (widthParent > 0) {
                mLayoutParams.width = (int) LayoutUtils.getInstance(getContext()).getWidth(widthParent);
            }
            if (heightParent > 0) {
                mLayoutParams.height = (int) (LayoutUtils.getInstance(getContext()).getHeight(heightParent));
            }
            if (topMargin > 0) {
                mLayoutParams.topMargin = (int) (LayoutUtils.getInstance(getContext()).getHeight(topMargin));
            }
            if (bottomMargin > 0) {
                mLayoutParams.bottomMargin = (int) (LayoutUtils.getInstance(getContext()).getHeight(bottomMargin));
            }
            if (rightMargin > 0) {
                mLayoutParams.rightMargin = (int) (LayoutUtils.getInstance(getContext()).getWidth(rightMargin));
            }
            if (leftMargin > 0) {
                mLayoutParams.leftMargin = (int) (LayoutUtils.getInstance(getContext()).getWidth(leftMargin));
            }
        }

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new LayoutParams(getContext(), attrs);
    }

    private static class LayoutParams extends RelativeLayout.LayoutParams {
        private float widthParent = 0;
        private float heightParent = 0;
        private float topMargin = 0;
        private float bottomMargin = 0;
        private float rightMargin = 0;
        private float leftMargin = 0;


        public float getWidthParent() {
            return widthParent;
        }

        public float getHeightParent() {
            return heightParent;
        }

        public float getTopMargin() {
            return topMargin;
        }

        public float getBottomMargin() {
            return bottomMargin;
        }

        public float getRightMargin() {
            return rightMargin;
        }

        public float getLeftMargin() {
            return leftMargin;
        }


        public LayoutParams(Context c, AttributeSet attrs) {
            super(c, attrs);
            TypedArray mArray = c.obtainStyledAttributes(attrs, R.styleable.CustomRelativelayout);
            widthParent = mArray.getFloat(R.styleable.CustomRelativelayout_layout_widthSize, this.getWidthParent());
            heightParent = mArray.getFloat(R.styleable.CustomRelativelayout_layout_heightSize, this.getHeightParent());
            topMargin = mArray.getFloat(R.styleable.CustomRelativelayout_layout_topMarginSize, this.getTopMargin());
            bottomMargin = mArray.getFloat(R.styleable.CustomRelativelayout_layout_bottomMarginSize, this.getBottomMargin());
            rightMargin = mArray.getFloat(R.styleable.CustomRelativelayout_layout_rightMarginSize, this.getRightMargin());
            leftMargin = mArray.getFloat(R.styleable.CustomRelativelayout_layout_leftMarginSize, this.getLeftMargin());
        }
    }
}
